package com.panshi.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.panshi.auth.JWT;
import com.panshi.model.UcenterMember;
import com.panshi.tools.ResponseData;
import com.panshi.tools.StringTools;

public class LoginInterceptor implements Interceptor {
    public void intercept(Invocation inv) {
        Controller controller = inv.getController();// 获得controller信息
        String token = controller.getPara("token");

        if (!StringTools.isEmptyString(token)) {
            UcenterMember ucentermember = JWT.unsign(token, UcenterMember.class);
            if (ucentermember != null){
//            controller.setSessionAttr("ucentermember",ucentermember);
                controller.setAttr("ucentermember",ucentermember);
                inv.invoke();
            }
            else controller.renderJson(ResponseData.forbidden());
            return;
        }
        controller.renderJson(ResponseData.forbidden());
    }
}
